package fasilkom.antz.sprite;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;

import fasilkom.antz.GameChar;
import fasilkom.antz.Stage;


public class CharacterProfile extends SpriteGroup {
	
	private BufferedImage[] icons;
	
	private Sprite avatar;
	private Sprite typeIcon;
	
	private Sprite potion;
	private FontSprite fontPotion;
	private Sprite energy_potion;
	private FontSprite fontEnergyPotion;
	private Sprite attack_up;
	private Sprite defense_up;
	
	private FontSprite typeText;
	private FontSprite healthText;
	private BufferedImage healthBar;
	private FontSprite energyText;
	private BufferedImage energyBar;
	
	private float health;
	private float energy;
	private boolean visible = false;
	
	private Stage scene;

	public CharacterProfile(Stage scene, int x, int y) {
		super("Character Profile Group");
		
		this.scene = scene;
		
		/*GameFont font = scene.fontManager.getFont(scene.getImages
				("assets/font-small.png", 13, 6), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789   .,;:!?\"-/&`' ");*/
		
		GameFont font = scene.fontManager.getFont(new Font("Kristen ITC", Font.BOLD, 14));
		
		// Initiate components
		
		avatar = new Sprite(x+20, y+30);
		
		healthText = new FontSprite("", font);
		healthText.setLocation(x, y + 90);
		healthBar = scene.getImage("assets/health-bar.png");
		
		energyText = new FontSprite("", font);
		energyText.setLocation(x, y + 120);
		energyBar = scene.getImage("assets/energy-bar.png");
		
		typeIcon = new Sprite(x, y + 155);
		typeText = new FontSprite("", font);
		typeText.setLocation(x + 30, y + 165);
		
		potion = new Sprite(x+10, y + 410);
		potion.setImage(scene.getImage("assets/potion.png"));
		fontPotion = new FontSprite("0", font);
		fontPotion.setLocation(x+15, 445);
		
		energy_potion = new Sprite(x+10, y + 455);
		energy_potion.setImage(scene.getImage("assets/energy.png"));
		fontEnergyPotion = new FontSprite("0", font);
		fontEnergyPotion.setLocation(x+15, 490);
		
		// Add to this group
		
		add(avatar);
		add(typeIcon);
		add(typeText);
		add(healthText);
		add(energyText);
		add(potion);
		add(fontPotion);
		add(energy_potion);
		add(fontEnergyPotion);
		
		// Initiate icons
		
		icons = scene.getImages("assets/icon-types.png", 4, 1);
	}
	
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	/**
	 * Update tampilan profil karakter.
	 * @param c
	 * @param avatar
	 */
	public void update(GameChar c, BufferedImage avatar) {
		this.health = (float) c.status.health / c.status.maxHealth;
		this.energy = (float) c.status.energy / c.status.maxEnergy;
		this.avatar.setImage(avatar);
		
		healthText.setText(String.valueOf(c.status.health));
		energyText.setText(String.valueOf(c.status.energy));
		typeIcon.setImage(icons[c.status.type]);
		typeText.setText(c.status.getTypeString());
		
		if(c.status.energy >= c.status.energyUsed*2){
			itemClicked(c,potion,"potion");
			itemClicked(c,energy_potion,"energy");
		}
		
		fontPotion.setText(String.valueOf(scene.main.healthPotion));
		fontEnergyPotion.setText(String.valueOf(scene.main.energyPotion));
	}
	
	@Override
	public void render(Graphics2D g) {
		if (!visible)
			return;
		
		super.render(g);
		
		potion.render(g);
		fontPotion.render(g);
		
		energy_potion.render(g);
		fontEnergyPotion.render(g);
		
		// posisi acuan, yakni posisi panel ini
		int x = (int) (getBackground().getX() + getBackground().getClip().x);
		int y = (int) (getBackground().getY() + getBackground().getClip().y);
		
		// health dan energy bar bukan sprite sehingga harus dirender manual
		g.drawImage(healthBar, x + (int) healthText.getX(),
				y + (int) healthText.getY() + healthText.getHeight(),
				(int) (healthBar.getWidth() * health),
				healthBar.getHeight(), null);
		g.drawImage(energyBar, x + (int) energyText.getX(),
				y + (int) energyText.getY() + energyText.getHeight(),
				(int) (energyBar.getWidth() * energy),
				energyBar.getHeight(), null);
	}
	
	public void itemClicked(GameChar c, Sprite item, String itemName){
		
		int x0 = (int) item.getScreenX();
		int y0 = (int) item.getScreenY();
		int x1 = x0 + item.getWidth();
		int y1 = y0 + item.getHeight();
		
		if (scene.click() && scene.getMouseX() > x0 && scene.getMouseX() < x1
				&& scene.getMouseY() > y0 && scene.getMouseY() < y1 ) {
			scene.bsSound.play("assets/nambahdarah.mp3");
				useItem(itemName,c);
		}
	}
	
	public void useItem(String item, GameChar c){
	//	c.status.energy -= 2* c.status.energyUsed;
		
		if(item.equals("potion") && scene.main.healthPotion > 0){
			scene.animateAttack(scene.getSelectedCharacter(), scene.saveEffect);
			c.status.health += 50;
			c.status.energy -= 2* c.status.energyUsed;
			scene.main.healthPotion--;
			if(c.status.health > c.status.maxHealth){
				c.status.health = c.status.maxHealth;
			}
		}
		else if(item.equals("energy") && scene.main.energyPotion > 0){
			scene.animateAttack(scene.getSelectedCharacter(), scene.saveEffect);
			c.status.energy += 100;
			c.status.energy -= 2* c.status.energyUsed;
			scene.main.energyPotion--;
			if(c.status.energy > c.status.maxEnergy){
				c.status.energy = c.status.maxEnergy;
			}
		}
	}
}
